home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / QuickDraw / Icon Display / bullWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-05  |  4.4 KB  |  227 lines  |  [TEXT/KAHL]

  1. #include "QDOffscreen.h"
  2.  
  3. /*****
  4.  * bullWindow.c
  5.  *
  6.  *        The window routines for the Bullseye demo
  7.  *
  8.  *****/
  9.  
  10. WindowPtr    bullseyeWindow;
  11. Rect        dragRect;
  12. Rect        windowBounds = { 40, 40, 300, 500 };
  13. Rect        circleStart = {10, 10, 100, 100};
  14. int            width = 5;
  15.  
  16. /****
  17.  * SetUpWindow()
  18.  *
  19.  *    Create the Bullseye window, and open it.
  20.  *
  21.  ****/
  22.  
  23. SetUpWindow()
  24.  
  25. {
  26.     dragRect = screenBits.bounds;
  27.     
  28.     bullseyeWindow = NewWindow(0L, &windowBounds, "\pBullseye", true, noGrowDocProc,(WindowPtr)-1L, true, 0);
  29.     SetPort(bullseyeWindow);
  30. }
  31. /* end SetUpWindow */
  32.  
  33. #define BYTESIZE 8
  34. #define WIDTHHEIGHT 16
  35.  
  36. GWorldPtr    GgwPtr = NULL;
  37. Rect    PlotRect = {10,200,26,216};
  38.  
  39. Boolean RectNotInGrayRgn(r)
  40. Rect *r;
  41. {
  42.     RgnHandle rgn = GetGrayRgn();
  43.     Rect    IRect,wRect;
  44.     
  45.     IRect = (**rgn).rgnBBox;
  46.     SectRect(&IRect,r,&wRect);
  47.     
  48.     return EmptyRect(&wRect);
  49. }
  50.  
  51. FindNewDevice(wind)
  52. WindowPtr    wind;
  53. {
  54.     Rect    gRect,lRect,wRect;
  55.     GDHandle    aDevice;
  56.     GDHandle    dominateDevice = NULL;
  57.     long        gArea,cArea;
  58.     GrafPtr        gp;
  59.     RgnHandle    rgn;
  60.     
  61.     GetPort(&gp);
  62.     SetPort(wind);
  63.     
  64.     gRect = PlotRect;
  65.     LocalToGlobal((Point *)&gRect.top);
  66.     LocalToGlobal((Point *)&gRect.bottom);
  67.     
  68.     aDevice = GetDeviceList();
  69.     gArea = cArea = 0;
  70.     while ( aDevice ) {
  71.         lRect = (**aDevice).gdRect;
  72.         SectRect(&lRect,&gRect,&wRect);
  73.         
  74.         if ( !EmptyRect(&wRect) ) {
  75.             cArea = (long)(wRect.right - wRect.left) * (long)(wRect.bottom - wRect.top);
  76.         
  77.             if ( cArea > gArea ) {
  78.                 gArea = cArea;
  79.                 dominateDevice = aDevice;
  80.             }
  81.         }
  82.         
  83.         aDevice = GetNextDevice(aDevice);
  84.     }
  85.     
  86.     if ( GgwPtr && dominateDevice ) {
  87.         if ( (**((**dominateDevice).gdPMap)).pixelSize != (**(GgwPtr->portPixMap)).pixelSize)
  88.             InvalRect(&PlotRect);
  89.     }
  90.     SetPort(gp);
  91. }
  92.         
  93.         
  94.     
  95.     
  96.  
  97. MakeANewGWorld(Rect *r)
  98. {
  99.     Rect        localR;
  100.     Handle        h;
  101.     QDErr        err;
  102.     short        i;
  103.     Ptr            p,tp;
  104.     short        depth;
  105.     
  106.     
  107.     localR = *r;
  108.     
  109.     /* 
  110.        by passing a global rectangle we force NewGWorld to determine the resolution
  111.        of the device the the majority of the rectangle lies on.  Then we can use the pixel
  112.        depth to determine which icl or ics to get
  113.     */
  114.     LocalToGlobal((Point *)&localR.top);
  115.     LocalToGlobal((Point *)&localR.bottom);
  116.     
  117.     if ( RectNotInGrayRgn(&localR) )
  118.         return;
  119.     if ( !GgwPtr ) {
  120.         if ( err = NewGWorld(&GgwPtr,0,&localR,NULL,NULL,noNewDevice) )
  121.             return;
  122.     }
  123.     else
  124.     if ( err = UpdateGWorld(&GgwPtr,0,&localR,NULL,NULL,0) )
  125.            return;
  126.  
  127.         
  128.     /* now grab the right depth of icl or ics */
  129.         depth = (**(GgwPtr->portPixMap)).pixelSize; 
  130.         switch ( depth ) {
  131.              
  132.             case 2: SetRect(&localR,0,0,16,16);
  133.                     DisposeGWorld(GgwPtr);
  134.                     if ( err = NewGWorld(&GgwPtr,1,&localR,NULL,NULL,0) )
  135.                            return; 
  136.             case 1: h = GetResource('ics#',128);
  137.                     depth = 1;
  138.                     break;
  139.             case 4:    h = GetResource('ics4',128);
  140.                     break;
  141.             case 8:    h = GetResource('ics8',128);
  142.                     break;
  143.             default: h = GetResource('ics8',128);
  144.                      depth = 8;
  145.                      SetRect(&localR,0,0,16,16);
  146.                      DisposeGWorld(GgwPtr);
  147.                      if ( err = NewGWorld(&GgwPtr,8,&localR,NULL,NULL,0) )
  148.                            return; 
  149.                      break;
  150.         }
  151.         
  152.  
  153.         
  154.         
  155.         /* we will be working with the pixel data directly so lock them */
  156.         p = GetPixBaseAddr(GgwPtr->portPixMap);
  157.         if ( p ) {
  158.             HLock(h);
  159.             tp = *h;
  160.             
  161.             /* 
  162.                 because NewGWorld usually adds a long word of padding so that
  163.                 it can more easily keep the pixel map long word aligned we
  164.                 can't just BlockMove the data directly.  Instead we have to
  165.                 move one row from our source and then advance the pointer of the
  166.                 destination to the next row
  167.             */
  168.             for (  i = 0; i < WIDTHHEIGHT; i++ ) { 
  169.                 BlockMove(tp,p,(WIDTHHEIGHT / BYTESIZE) * depth);        /* move a row */
  170.                 /*
  171.                   now advance the destination pointer to the next row.  Don't
  172.                   forget to and off he high bit which marks this as a pixelmap
  173.                  */
  174.                 p += ((**(GgwPtr->portPixMap)).rowBytes ) & 0x7fff;
  175.                 tp += (WIDTHHEIGHT / BYTESIZE) * depth; /*move the source pointer to the next row */
  176.             }
  177.             HUnlock(h);
  178.             ReleaseResource(h);
  179.         }        
  180. }
  181.  
  182. DrawIcResources()
  183. {
  184.     Rect    r,r1;
  185.     QDErr    err;
  186.     Ptr        p;
  187.     long    numpixels;
  188.     short    i;
  189.     Ptr        tp;
  190.     
  191.     
  192.         
  193.     
  194.     r1 = PlotRect;
  195.     MakeANewGWorld(&r1);
  196.         
  197.     if ( GgwPtr ) {
  198.         r1 = (**(GgwPtr->portPixMap)).bounds;
  199.         r1.right = r1.left + 16;
  200.         CopyBits((BitMap *)(*(GgwPtr->portPixMap)),&bullseyeWindow->portBits,
  201.                     &(r1),&PlotRect,srcCopy,NULL);
  202.     }
  203. }
  204.         
  205.  
  206. /*****
  207.  * DrawBullseye()
  208.  *
  209.  *    Draws the bullseye.
  210.  *
  211.  *****/
  212.  
  213. DrawBullseye(active)
  214.  
  215.     short    active;
  216.  
  217. {
  218.     
  219.     SetPort(bullseyeWindow);
  220.     EraseRect(&bullseyeWindow->portRect);
  221.     
  222.     
  223.     
  224.     DrawIcResources();
  225.  
  226. }
  227. /* end DrawBullseye */